package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.enums.UserHelpStatusDictEnum;
import com.qqtech.qquan.user.model.UserHelp;
import com.qqtech.qquan.user.model.UserHelpQuery;
import com.qqtech.qquan.user.model.UserHelpVo;
import com.qqtech.qquan.user.service.UserHelpService;

@Controller
@RequestMapping(value = "/user/help")
public class UserHelpController extends BaseAdminController {

	@Resource
	private UserHelpService userHelpService;
	
	@Override
	protected BaseService<UserHelp> getBaseService() {
		return userHelpService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/user/help/userHelp-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserHelpQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserHelpVo> page = userHelpService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(page.getNumberOfElements());
		return adminPage;
	}
	
	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/user/help/userHelp-add";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param add
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(UserHelp userHelp) {
		Result ret = new Result();
		userHelpService.insert(userHelp);
		return ret;
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		UserHelpVo userHelpVo = userHelpService.getById(id);
		model.addAttribute("userHelpVo", userHelpVo);
		return "/user/help/userHelp-update";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(UserHelp userHelp) {
		Result ret = new Result();
		userHelpService.updateById(userHelp);
		return ret;
	}
	
	/**
	 * 恢复
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/receive")
	@ResponseBody
	public Result receive(Integer id) {
		Result ret = new Result();
		UserHelpVo userHelpVo =new UserHelpVo();
		userHelpVo.setId(id);
		userHelpVo.setStatus(UserHelpStatusDictEnum.NORMAL.getCode());
		userHelpService.updateById(userHelpVo);
		return ret;
	}
	
	/**
	 * 删除
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/unclaimed")
	@ResponseBody
	public Result unclaimed(Integer id) {
		Result ret = new Result();
		UserHelpVo userHelpVo =new UserHelpVo();
		userHelpVo.setId(id);
		userHelpVo.setStatus(UserHelpStatusDictEnum.DELETE.getCode());
		userHelpService.updateById(userHelpVo);
		return ret;
	}
}
